package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.UserCommunication;
import com.digital.welcome.mapper.UserCommunicationMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserCommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UserCommunicationServiceImpl extends ServiceImpl<UserCommunicationMapper, UserCommunication> implements UserCommunicationService {

    @Autowired
    private UserCommunicationMapper userCommunicationMapper;

    @Override
    public String getIdSophomoreByUserName(String userName) {
        UserCommunication userCommunication = userCommunicationMapper.getIdSophomoreByUserName(userName);
        if (userCommunication == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("该新同学没有相匹配的高年级学生"));
        }
        return JSON.toJSONString(new Result<>().setCode(200).setData(userCommunication).setMessage("查询成功"));
    }

    @Override
    public String getIdFreshmanByUserName(String userName) {
        UserCommunication userCommunication = userCommunicationMapper.getIdFreshmanByUserName(userName);
        if (userCommunication == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("该高年级学生没有相匹配的新同学"));
        }
        return JSON.toJSONString(new Result<>().setCode(200).setData(userCommunication).setMessage("查询成功"));
    }

    @Override
    public String getIdByUserName(String userName) {
        Pattern p = Pattern.compile("^2019\\w+$");
        Matcher matcher = p.matcher(userName);
        UserCommunication userCommunication;

        if (matcher.find()) {
            userCommunication = userCommunicationMapper.getIdSophomoreByUserName(userName);
        } else {
            userCommunication = userCommunicationMapper.getIdFreshmanByUserName(userName);
        }
        if (userCommunication==null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("无该用户所对应的交流ID"));
        }
        return JSON.toJSONString(new Result<>().setCode(200).setData(userCommunication).setMessage("查询成功"));
    }
}
